package basic.courseLearn08.problem;

import org.junit.Test;

import java.util.Comparator;
import java.util.PriorityQueue;

//字符串拼接，要求对所有字符串进行拼接，要求最后拼接之后的字符串最大
public class StringSplice {

    public String splice(String[] strs) {
        //1. 定义大根堆
        PriorityQueue<String> maxHeap = new PriorityQueue<String>(new StringComparator());
        String result = "";
        //2. 遍历字符串数组，将每个字符串放入大根堆
        for (String str : strs) {
            maxHeap.add(str);
        }
        //3. 循环
        for (int i = 0; i < strs.length; i++) {
            //取出堆顶
            result+=maxHeap.poll();
        }

        return result;
    }

    public class StringComparator implements Comparator<String> {

        @Override
        public int compare(String o1, String o2) {
            String s1 = o1 + o2;
            String s2 = o2 + o1;
            if (s1.compareTo(s2) > 0){

                return -1;
            }
            return 1;
        }
    }


    @Test
    public void test() {
        String[] strs = {"aa", "ab", "aa"};
        String result = splice(strs);
        System.out.println(result);
    }





}
